<template>
  <div v-loading="true" v-if="loading" class="detail-wapper"/>
  <div v-else class="detail-wapper">
    <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" class="detail-wapper__form">
      <!-- <el-form-item label="跑马灯名称" prop="name">
        <el-input v-model="formData.name" :max-length="100" placeholder="填写账户名称"/>
      </el-form-item> -->
      <el-form-item label="跑马灯内容" prop="content">
        <el-input :rows="6" v-model="formData.content" :max-length="300" type="textarea" placeholder="填写跑马灯名称"/>
      </el-form-item>
      <el-form-item label="跑马灯时间" prop="timeLimit">
        <el-date-picker
          v-model="formData.timeLimit"
          type="datetimerange"
          value-format="yyyy-MM-dd hh:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { parseTime } from '@/utils'
import { updateMarquee, getMarqueeDetail } from '@/api/marquee'
const TYPE = 2
export default {
  name: 'MarqueeUpdate',
  data() {
    return {
      loading: true,
      formData: {
        name: '',
        content: '',
        timeLimit: []
      },
      rules: {
        // name: [
        //   { required: true, message: '请填写角色名称', trigger: 'blur' }
        // ],
        content: [
          {
            required: true, message: '请填写跑马灯内容', trigger: 'blur'
          }
        ],
        timeLimit: [
          {
            required: true, trigger: 'change',
            validator: (rule, value, callback) => {
              var { timeLimit = [] } = this.formData
              if (timeLimit && timeLimit.length) {
                callback()
              } else {
                callback('请选择发布时间')
              }
            }
          }
        ]
      },
      resourceTree: [],
      defaultProps: {
        children: 'children',
        label: 'resourceName'
      },
      defaultCheckIds: []
    }
  },
  async created() {
    var { advertiseId = '' } = this.$route.query
    this.loading = true
    var getMarqueeDetailPromise = getMarqueeDetail({ id: advertiseId })
    try {
      const {
        name,
        content,
        startPublishTime,
        endPublishTime
      } = await getMarqueeDetailPromise
      var timeLimit = [parseTime(new Date(startPublishTime), '{y}-{m}-{d} {h}:{i}:{s}'), parseTime(new Date(endPublishTime), '{y}-{m}-{d} {h}:{i}:{s}')]
      this.formData = Object.assign(this.formData, {
        name,
        content,
        timeLimit
      })
    } catch (exp) {
      //

    }
    this.loading = false
  },
  methods: {
    _beforeCheck(formName) {
      return new Promise(resolve => {
        this.$refs[formName].validate((valid) => {
          resolve(valid)
        })
      })
    },
    async submitForm(formName) {
      var check = await this._beforeCheck(formName)
      var { advertiseId = '' } = this.$route.query || {}
      var {
        name = '',
        content = '',
        timeLimit: [startPublishTime, endPublishTime] = []
      } = this.formData || {}

      if (check) {
        updateMarquee({ advertiseId, name, content, startPublishTime, endPublishTime, type: TYPE }, '修改跑马灯失败').then(() => {
          this.$message({
            type: 'success',
            message: '修改跑马灯成功'
          })
          this.$router.replace({ name: 'MarqueeManage' })
        }).catch(msg => {
          this.$message({
            type: 'error',
            message: msg
          })
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
.detail-wapper{
    padding:30px;
    &__form{
        width:500px;
        margin:0 auto;
    }
}
</style>
<style lang="scss">
.detail-wapper{
    padding:30px;
    &__form{
        .el-checkbox{
             margin-right:30px;
        }
        .el-checkbox+.el-checkbox{
            margin-left:0;

        }
    }
}
</style>

